package net.easyunion.dataservice.dao.impl;

import java.beans.PropertyVetoException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import javax.annotation.Resource;

import net.easyunion.dataservice.common.tool.ApplicationConfig;
import net.easyunion.dataservice.common.tool.JdbcTemplateUtil;
import net.easyunion.dataservice.dao.DataManager;
import net.easyunion.dataservice.vo.JdbcTemplateVo;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 
 * @author lipenghui
 * 
 * @version 创建时间：2013年11月11日 下午3:20:02
 */
@Service("dataManager")
public class JdbcDataManager implements DataManager {

	@Resource
	private JdbcTemplate euds_db;

	public int executeSql(String sql, Object[] obj) {
		// TODO Auto-generated method stub
		this.euds_db.queryForInt("select 1;");
		return 0;
	}

	public Map<String, Object> querySql(String sql, Object[] obj) {
		// TODO Auto-generated method stub

		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void loadDBSource() {
		// TODO Auto-generated method stub
		String sql = "select * from datasource_prop;";
		String sqlCount = "select count(*) from datasource_prop;";
		int total = this.euds_db.queryForInt(sqlCount);
		try{
			if (total > 0) {
				this.euds_db.query(sql.toString(), new RowMapper() {
					public Object mapRow(ResultSet rs, int arg1)
							throws SQLException {
						JdbcTemplateVo vo = new JdbcTemplateVo();
						vo.setDsId(rs.getInt("ds_id"));
						vo.setJdbcClassName(rs.getString("ds_driver_class_name"));
						vo.setJdbcParam(rs.getString("ds_jdbc_param"));
						vo.setJdbcUrl(rs.getString("ds_jdbc_url"));
						vo.setUsername(rs.getString("ds_username"));
						vo.setPassword(rs.getString("ds_password"));
						vo.setInfo(rs.getString("info"));
						try {
							JdbcTemplateUtil.putJdbcTemplateToCache(vo);
						} catch (PropertyVetoException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						return vo;
					}
				});
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

}
